package com.app.web.user.info;

import com.app.utils.base.http.ResultUtil;
import com.app.utils.spring.properties.Config;
import com.app.web.user.login.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Description
 * Created by 王伟
 * Date create on 14:26 2019/11/19.
 */
@RestController
@RequestMapping("/user/info/")
public class UserInfoController{
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private LoginService loginService;

    /**
     * 功能：注册用户
     * @param uid 用户id
     * @param mcode 推荐码
     * @param ip 注册用户ip
     * @param app 1=安卓 2=苹果 5=小程序
     * @return
     */
//    @RequestMapping("register")
//    public String addUser(@RequestAttribute int uid, @RequestParam(defaultValue="0")int mcode, @RequestAttribute String ip, @RequestParam(defaultValue="0") int app){
//        //获取登录信息
//        LoginInfo loginInfo = loginService.info(uid);
//        //注册用户信息
//        UserInfo userInfo = userInfoService.addInfo(uid, mcode, ip);
//        //返回信息
//        Map<String,Object> data = new HashMap<>();
//        data.put("type",2);
//        data.put("user",loginService.loginUser(loginInfo,userInfo));
//        return ResultUtil.success(data);
//    }

    /**
     * 刷新用户信息
     * @param uid
     * @return
     */
    @RequestMapping("refresh")
    private String refresh(@RequestAttribute int uid){
        //用户信息
        UserInfo userInfo = userInfoService.info(uid);
        if(userInfo.getRole()!=3){
            ResultUtil.error(8014,"您无法进行该操作");
        }
        Map<String,Object> data = ResultUtil.result();
//        //发送升级消息
        data.put("msg", "用户信息已更新！");
//        //返回跳转专属客户
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("type",2);
        Map<String,Object> userData = new HashMap<>();
        userData.put("role",userInfo.getRole());
        userData.put("roleName",userInfo.getRoleName());
        userData.put("roleImg", Config.pp.getOrDefault("user.roleImg", "https://aloss.taomeimei.net/front/applet/img/common/level-")+userInfo.getRole()+".png");
        resultData.put("user",userData);
        data.put("data",resultData);
        return ResultUtil.success(data);
    }
}
